const { CreateChar, selectChat } = require('../services/ChatService')

class Chatcontroller {
    //聊天记录存入数据库
    async ChatRecord(params) {
        const res = await CreateChar(params)
        return res
    }

    //查询聊天记录
    async Chatrefer(ctx, next) {
        try {
            const { user_id } = ctx.state.user
            const { friendId, pageNo, pageSize } = ctx.request.query
            const params = { senderId: user_id, receiverId: friendId, pageNo, pageSize }
            const res = await selectChat(params);
            res.rows.forEach((item) => {
                if (item.type == 2) {
                    const { image } = JSON.parse(item.message)
                    item.message = { image }
                }
                else if (item.type == 3) {
                    item.message = JSON.parse(item.message)
                }
            })
            ctx.body = {
                code: 0,
                message: 'success',
                result: res.rows,
                count: res.count
            }
        } catch (error) {
            console.log(error);
        }

    }
}


module.exports = new Chatcontroller()